package com.studyv3.test.controller.productsbuy;


import com.studyv3.common.core.controller.BaseController;
import com.studyv3.common.core.domain.AjaxResult;
import com.studyv3.common.core.page.TableDataInfo;
import com.studyv3.common.utils.PageUtils;
import com.studyv3.test.domain.ApplyProduct;
import com.studyv3.test.domain.Products;
import com.studyv3.test.service.ApplyProductService;
import com.studyv3.test.service.ProductsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * lb_购买申请/审批 前端控制器
 * </p>
 *
 * @author author
 * @since 2024-05-07
 */
@RestController
@RequestMapping("/applyproduct/")
public class ApplyProductController extends BaseController {

@Autowired
    ProductsService productsService;
    @Autowired
    private ApplyProductService applyProductService;

    /**
     * 查询我的申请
     * **/
    @GetMapping("mylist")
    public TableDataInfo applyMyProducts(ApplyProduct applyProduct) {
        applyProduct.setApplicant(getUserId().intValue());
        applyProduct.setApplicantName(getUsername());
        List<ApplyProduct>list = applyProductService.selectMyApplyProductList(applyProduct);
        return getDataTable(list);
    }

    /**
     * 查询申请
     * **/
    @GetMapping("list")
    public TableDataInfo applyProducts(ApplyProduct applyProduct) {
        applyProduct.setDeleted(0);
        PageUtils.startPage();
        List<ApplyProduct>list = applyProductService.selectApplyProductList(applyProduct);
        return getDataTable(list);
    }

    @PostMapping("add")
    public AjaxResult add(@RequestBody ApplyProduct applyProduct) {
        applyProduct.setApplicant(getUserId().intValue());
        return applyProductService.addApplyProduct(applyProduct);
    }

    @PostMapping("update")
    public AjaxResult update(@RequestBody ApplyProduct applyProduct) {
        return applyProductService.updateApplyProduct(applyProduct);
    }

    @DeleteMapping("del/{ids}")
    public AjaxResult del(@PathVariable Integer[] ids) {
        return applyProductService.deleteApplyProduct(ids);
    }

    @PostMapping("pricebyid")
    public AjaxResult pricebyid(@RequestBody Products products) {
        AjaxResult ajaxResult = AjaxResult.success();
        products = productsService.productsById(products.getId());
        return ajaxResult.put("price", productsService.productsById(products.getId()).getPrice());

    }

}
